﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;
using TMDT.Web.UI.Commons;

namespace TMDT.Web.UI.Controls
{
    public partial class HinhAnhControl : System.Web.UI.UserControl
    {
        private static string FileName { get; set; }
        //private static string SanPham { get; set; }
        private static string HinhAnh { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if( ! IsPostBack)
            {
                LoadData();
            }
        }

        #region Methods

        private void LoadData()
        {
            mtvView.ActiveViewIndex = 0;

            LoadRepeater();
            LoadDropDownList();
        }

        private void LoadRepeater()
        {
            rptHinhAnh.DataSource = new HinhAnhManager().GetHinhAnh();
            rptHinhAnh.DataBind();
        }

        private void LoadDropDownList()
        {
            //ddlSanPham.LoadDataSource(new SanPhamManager().GetSanPham(), "TenSanPham", "IDSanPham");
        }

        private void LoadUpdate()
        {
            //var sanPham = new SanPhamManager().GetSanPham(SanPham);
            lblTenSanPhamUpdate.Text = new HinhAnhManager().GetHinhAnh(HinhAnh).IDSanPham.ToString();
        }

        private void Cancel()
        {
            mtvView.ActiveViewIndex = 0;
        }

        private void Update()
        {
            if (fupURL.FileName == "")
                return;
            var entity = new HinhAnhManager().GetHinhAnh(HinhAnh);
            entity.URL = fupURL.FileName;

            new HinhAnhManager().Update(entity);

        }

        #endregion

        protected void lbnUpdate_Click(object sender, EventArgs e)
        {
            Update();
        }

        protected void imgEdit_Click(object sender, ImageClickEventArgs e)
        {
            var idHinhAnh = ((ImageButton) sender).CommandArgument;

            HinhAnh = idHinhAnh;

            LoadUpdate();

            mtvView.ActiveViewIndex = 1;

        }
        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            var id = ((ImageButton) sender).CommandArgument;
            var service = new HinhAnhManager(); 
            var entity = service.GetHinhAnh(id);

            service.Remove(entity);

            LoadRepeater();

            Cancel();
        }

        protected void lbnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }
    }
}